package com.zelin;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.hibernate.validator.constraints.br.TituloEleitoral;
import org.junit.Test;

import java.util.Date;

/**
 * Created by WF on 2020/4/3 15:36
 * jwt的一些高级用法
 */
public class JWTAdvDemo {
    //1.创建jwt的token，此时带有过期时间
    @Test
    public void test01(){
        //1.1）定义过期时间
        long l = System.currentTimeMillis();
        Date dt = new Date(l + 60 * 1000);     //设置过期时间为1分钟
        //1.2)定义jwtBuilder生成器对象
        JwtBuilder jwtBuilder =
                Jwts.builder().setId("8888")
                .setSubject("user")
                .setIssuedAt(new Date())
                .setExpiration(dt)
                .signWith(SignatureAlgorithm.HS256, "zelin");
        //1.3)创建token对象
        String token = jwtBuilder.compact();
        System.out.println("token = " + token);
    }
    //2.解析带有过期时间的token
    @Test
    public void test02(){
        //2.1)定义要解析的token对象
        String token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI4ODg4Iiwic3ViIjoidXNlciIsImlhdCI6MTU4NTg5OTgxMCwiZXhwIjoxNTg1ODk5ODcwfQ.KSXMSETENglV85H-jA95Qs5VrKwRjnlM8jKVn_eofpw";
        //2.2)解析生成claims对象
        Claims claims = Jwts.parser().setSigningKey("zelin").parseClaimsJws(token).getBody();
        //2.3)生成解析结果
        String id = claims.getId();
        Date issuedAt = claims.getIssuedAt();
        String subject = claims.getSubject();
        Date expiration = claims.getExpiration();
        System.out.println("id = " + id);
        System.out.println("issuedAt = " + issuedAt);
        System.out.println("subject = " + subject);
        System.out.println("expiration = " + expiration);
    }
    //3.定义携带数据的claims
    @Test
    public void test03(){
        //3.1)定义jwtBuilder生成器对象
        JwtBuilder jwtBuilder =
                Jwts.builder().setId("7777")
                        .setSubject("ttt")
                        .setIssuedAt(new Date())
                        .claim("roles","admin")
                        .signWith(SignatureAlgorithm.HS256, "zelin");
        //1.3)创建token对象
        String token = jwtBuilder.compact();
        System.out.println("token = " + token);
    }
    //4.解析携带的数据
    @Test
    public void test04(){
        //4.1)定义要解析的token
        String token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI3Nzc3Iiwic3ViIjoidHR0IiwiaWF0IjoxNTg1OTAwMTE2LCJyb2xlcyI6ImFkbWluIn0.qMDTWm8xY-NEC30TryOZ6Bbp2UOPMHmBkpVFS6oPlFQ";
        //4.2)解析token
        Claims claims = Jwts.parser().setSigningKey("zelin").parseClaimsJws(token).getBody();
        //4.3)得到数据
        String id = claims.getId();
        String subject = claims.getSubject();
        Date issuedAt = claims.getIssuedAt();
        String roles = (String) claims.get("roles");
        System.out.println("id = " + id);
        System.out.println("subject = " + subject);
        System.out.println("issuedAt = " + issuedAt);
        System.out.println("roles = " + roles);
    }
}
